import getMessage from "./message";
import store from "@/store";
import { Message } from "element-ui";
import { startLoading, endLoading } from "./loading";
function interceptor(instance) {
  instance.interceptors.response.use(
    (response) => {
      // if (response?.config?.isLoading) {
      //   // 请求显示loading 效果
      //   startLoading();
      // }
      const res = response.data;

      if (response?.config?.isDownLoadFile) {
        let fileName = "";
        const blob = new Blob([res], {
          type: "application/" + response.headers["content-type"],
        });
        const reader = new FileReader();
        reader.readAsText(blob);
        reader.onload = (resT) => {
          // 捕获服务端错误信息
          try {
            const _resData = JSON.parse(resT?.target?.result);
            Message({
              message: _resData.message || "无法下载",
              type: "error",
              duration: 2 * 1000,
            });
            endLoading();
          } catch (error) {
            const link = document.createElement("a");
            const href = window.URL.createObjectURL(blob);
            link.href = href;
            fileName = response.headers["content-disposition"]
              ?.split(";")[1]
              ?.split("=")[1];
            fileName = decodeURI(fileName);
            link.download = fileName;
            document.body.appendChild(link);
            link.click();
            window.setTimeout(() => {
              document.body.removeChild(link);
              window.URL.revokeObjectURL(href);
              if (response?.config?.isLoading) {
                endLoading();
              }
            });
          }
        };
        // let fileName = "";
        // const blob = new Blob([res], {
        //   type: "application/" + response.headers["content-type"],
        // });
        // const link = document.createElement("a");
        // const href = window.URL.createObjectURL(blob);
        // link.href = href;
        // fileName = response.headers["content-disposition"]
        //   ?.split(";")[1]
        //   ?.split("=")[1];
        // fileName = decodeURI(fileName);
        // link.download = fileName;
        // document.body.appendChild(link);
        // link.click();
        // window.setTimeout(() => {
        //   document.body.removeChild(link);
        //   window.URL.revokeObjectURL(href);
        //   if (response?.config?.isLoading) {
        //     endLoading();
        //   }
        // });
      } else {
        if (res.code == 30007 || res.code == 61000) {
          // user/userPermission 获取权限不足
          store.dispatch("auth/FedLogOut");
          if (response?.config?.isLoading) {
            endLoading();
          }
          return;
        }
        /* code为非200是抛错*/
        if (res.code !== 200) {
          getMessage("error", response);
          if (response?.config?.isLoading) {
            endLoading();
          }
          return Promise.reject(res);
        } else {
          /**code is 200 */
          getMessage("success", response);
          if (response?.config?.isLoading) {
            endLoading();
          }
          return res;
        }
      }
    },
    (error) => {
      // Message({
      //   message: error?.response?.data?.message ?? error.message,
      //   type: "error",
      //   duration: 3 * 1000,
      // });
      if (error?.config?.isLoading) {
        endLoading();
      }

      return Promise.reject(error);
    }
  );
}
export default interceptor;
